package parser

import (
	"bytes"
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"my-crawler/single-task/engine"
)

func GetCityUsersListParser(content []byte) engine.ParserResult {
	reader := bytes.NewReader(content)
	doc, err := goquery.NewDocumentFromReader(reader)
	if err != nil {
		fmt.Errorf("goquery: Load the HTML document failed! %v",err)
	}
	res := engine.ParserResult{}
	doc.Find(".posts .post .post-title a").Each(func(i int, s *goquery.Selection) {
		// For each item found, get the title
		title := s.Text()
		href, _ := s.Attr("href")
		res.Request = append(res.Request,engine.CrawlerRequest{
			href,
			func(c []byte) engine.ParserResult{
				return GetUserDetailParser(c,title)
			},
		})
		fmt.Printf("user_name:%s url:%s \n", title,href)
	})
	return res
}